import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../utils/http_helper.dart';
import '../screens/mine/model/gems_model.dart';

// 充值价格列表状态
class RechargeState {
  final List<GemsModel> gemsPackages;
  final List<GemsModel> vipPackages;
  final bool isLoading;
  final String? error;

  const RechargeState({
    this.gemsPackages = const [],
    this.vipPackages = const [],
    this.isLoading = false,
    this.error,
  });

  // 为了向后兼容，保留packages getter
  List<dynamic> get packages =>
      gemsPackages.isNotEmpty ? gemsPackages : vipPackages;

  RechargeState copyWith({
    List<GemsModel>? gemsPackages,
    List<GemsModel>? vipPackages,
    bool? isLoading,
    String? error,
  }) {
    return RechargeState(
      gemsPackages: gemsPackages ?? this.gemsPackages,
      vipPackages: vipPackages ?? this.vipPackages,
      isLoading: isLoading ?? this.isLoading,
      error: error,
    );
  }
}

// 充值价格列表Notifier
enum RechargeType { gems, vip }

class RechargeNotifier extends StateNotifier<RechargeState> {
  RechargeNotifier() : super(const RechargeState());

  Future<void> fetchRechargeData(RechargeType type) async {
    String url = type == RechargeType.gems
        ? 'ComicMember/GetRechargePriceLists'
        : 'ComicMember/GetVipPriceLists';

    if (type == RechargeType.gems) {
      await _fetchGemsData(url);
    } else {
      await _fetchVipData(url);
    }
  }

  Future<void> _fetchGemsData(String url) async {
    state = state.copyWith(isLoading: true, error: null);

    try {
      final response = await HttpUtil().getModel<List<GemsModel>>(
        url,
        fromJson: (json) =>
            (json as List).map((item) => GemsModel.fromJson(item)).toList(),
      );
      if (response?.status == 200 && response?.result != null) {
        state = state.copyWith(
          gemsPackages: response?.result ?? [],
          isLoading: false,
        );
      } else {
        state = state.copyWith(
          isLoading: false,
          error: response?.message ?? 'Failed to load gems packages',
        );
      }
    } catch (e) {
      state = state.copyWith(isLoading: false, error: '网络错误: $e');
    }
  }

  Future<void> _fetchVipData(String url) async {
    state = state.copyWith(isLoading: true, error: null);

    try {
      final response = await HttpUtil().getModel<List<GemsModel>>(
        url,
        fromJson: (json) =>
            (json as List).map((item) => GemsModel.fromJson(item)).toList(),
      );
      if (response?.status == 200 && response?.result != null) {
        state = state.copyWith(
          vipPackages: response?.result ?? [],
          isLoading: false,
        );
      } else {
        state = state.copyWith(
          isLoading: false,
          error: response?.message ?? 'Failed to load VIP packages',
        );
      }
    } catch (e) {
      state = state.copyWith(isLoading: false, error: '网络错误: $e');
    }
  }
}

// Provider
final rechargeProvider =
    StateNotifierProvider<RechargeNotifier, RechargeState>((ref) {
  return RechargeNotifier();
});
